﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;
using System.Configuration;

namespace Janksen.ValidationHelper
{
    /// <summary>
    /// 字符串验证配置帮助类
    /// </summary>
    internal class StringValidationConfigHelper
    {
        /// <summary>
        /// 配置节点
        /// </summary>
        internal class ConfigNode
        {
            private string m_Key;
            /// <summary>
            /// 配置文件中节点的键值
            /// </summary>
            public string Key
            {
                get
                {
                    return m_Key;
                }
                set
                {
                    m_Key = value;
                }
            }
            private string m_Value;
            /// <summary>
            /// 正则表达式的值
            /// </summary>
            public string Value
            {
                get
                {
                    return m_Value;
                }
                set
                {
                    m_Value = value;
                }
            }
            private string m_Message;
            /// <summary>
            /// 验证不通过的消息提示
            /// </summary>
            public string Message
            {
                get
                {
                    return m_Message;
                }
                set
                {
                    m_Message = value;
                }
            }
        }



        /// <summary>
        /// 获取xml配置节点
        /// </summary>
        /// <param name="xmlConfigNodeKey">节点键值</param>
        /// <returns>获取的节点ConfigNode</returns>
        public static ConfigNode GetXmlConfigNode(string xmlConfigNodeKey)
        {
            string xmlConfigPath = "";
            string xmlFilePath = "";

            try
            {
                xmlConfigPath = ConfigurationManager.AppSettings["ValidationHelper.StringValidationHelper.RegexConfig"].ToString();
            }
            catch
            {
                throw new ConfigurationErrorsException("缺少配置信息：ValidationHelper.StringValidationHelper.RegexConfig");
            }

            XmlDocument doc = new XmlDocument();

            xmlFilePath = HttpContext.Current.Server.MapPath(xmlConfigPath);

            try
            {
                doc.Load(xmlFilePath);
            }
            catch
            {
                throw new Exception(string.Format("找不到验StringValidationHelper配置文件：{0}" , xmlFilePath));
            }

            XmlNode node = doc.DocumentElement;
            XmlNode regex = node.SelectSingleNode("regex[@key='" + xmlConfigNodeKey + "']");

            if ( regex == null )
            {
                throw new Exception(string.Format("找不到配置节点：{0}" , xmlConfigNodeKey));
            }


            ConfigNode xmlConfigNode = new ConfigNode();
            xmlConfigNode.Key = xmlConfigNodeKey;

            try
            {
                xmlConfigNode.Value = regex.Attributes["value"].Value;
            }
            catch
            {
                throw new Exception(string.Format("找不到配置节点{0}的属性：value" , xmlConfigNodeKey));
            }

            try
            {
                xmlConfigNode.Message = regex.Attributes["message"].Value;
            }
            catch
            {
                throw new Exception(string.Format("找不到配置节点{0}的属性：message" , xmlConfigNodeKey));
            }

            return xmlConfigNode;
        }
    }
}
